package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.mapper.ActivityMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.crm.context.BaseContext;
import com.crm.dto.ActivityDto;
import com.crm.entity.Activity;

import com.crm.result.PageResult;
import com.crm.service.ActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper,Activity> implements ActivityService{
    @Autowired
    private ActivityMapper activityMapper;

    //活动分页查询
    @Override
    public PageResult getActivityPageWithConditions(ActivityDto activityDto) {
        Page<Activity> page = Page.of(activityDto.getPageNum(), activityDto.getPageSize());

        //分页条件
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<Activity>();
        wrapper.like(activityDto.getCode()!=null,Activity::getCode,activityDto.getCode());
        wrapper.eq(activityDto.getChannel()!=null,Activity::getChannel,activityDto.getChannel());
       //
//        if (!CollectionUtils.isEmpty(activityDto.getParams())){
//            wrapper.between(Activity::getCreateTime,activityDto.getParams().get("beginCreateTime"),activityDto.getParams().get("endCreateTime"));
//            wrapper.between(Activity::getActivityTime,activityDto.getParams().get("beginTime"),activityDto.getParams().get("endTime"));
//        }
        if (!CollectionUtils.isEmpty(activityDto.getParams())) {
            Object beginCreateTime = activityDto.getParams().get("beginCreateTime");
            Object endCreateTime = activityDto.getParams().get("endCreateTime");

            if (beginCreateTime != null && endCreateTime != null) {
                // 可选：进一步判断是否是空字符串
                if (!beginCreateTime.toString().trim().isEmpty() &&
                        !endCreateTime.toString().trim().isEmpty()) {
                    wrapper.between(Activity::getCreateTime, beginCreateTime, endCreateTime);
                }
            }

            // 同样的逻辑处理 ActivityTime
            Object beginTime = activityDto.getParams().get("beginTime");
            Object endTime = activityDto.getParams().get("endTime");

            if (beginTime != null && endTime != null &&
                    !beginTime.toString().trim().isEmpty() &&
                    !endTime.toString().trim().isEmpty()) {
                wrapper.between(Activity::getActivityTime, beginTime, endTime);
            }
        }


        super.page(page,wrapper);


        return new PageResult(page.getTotal(),page.getRecords());
    }

    //新增活动
    @Override
    public void add(ActivityDto activityDto) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDto,activity);
        //生成活动编码
        String randomUUID = UUID.randomUUID().toString();
        //只保留8位
        randomUUID = randomUUID.substring(0,8);
        //设置活动编码
        activity.setCode(randomUUID);
        //设置创建时间
        activity.setCreateTime(LocalDateTime.now().toString());
        //设置代金券金额
        activity.setVouchers(activityDto.getVouchers());
        //设置活动状态(默认设置为2启用）
        activity.setStatus(Activity.ACTIVITY_STATUS_OPEN);
        //设置创建人
        Long currentId = BaseContext.getCurrentId();
        activity.setCreateBy(currentId != null ? currentId.toString() : null);


        // 将 String[] 转换为 List<String>
        if (activityDto.getActivityTime() != null) {
            activity.setActivityTime(List.of(activityDto.getActivityTime()));
        }

        try {
            activityMapper.insert(activity);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //删除活动
    @Override
    public void delete(Long id) {
        activityMapper.deleteById(id);
    }

    //根据ID查询活动详细
    @Override
    public Activity selectById(Integer id) {
        Activity activity = activityMapper.selectById(id);
        return activity;
    }

    //修改活动信息
    @Override
    public void update(Activity activity) {
        activityMapper.updateById(activity);
    }

    @Override
    public List<Activity> selectAll(Long channelId) {
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(channelId.toString()),Activity::getChannel, channelId.toString());
        List<Activity> activities = activityMapper.selectList(wrapper);
        return activities;
    }


}
